package ru.papergames.battleserver.model;

import ru.papergames.battleserver.model.battle.npc.processors.NpcActionProcessor;
import ru.papergames.battleserver.network.PaperGameClient;
import ru.papergames.battleserver.network.serverpackets.flashuser.PaperGameServerPacket;
import ru.papergames.common.form.PcPlayerForm;

/**
 * @author Alexander Troshanin
 */
public class PaperGameFlashUser extends AbstractPaperGameUser<PcPlayerForm> {

    private PaperGameClient client;
    private long lastPingTime;

    public PaperGameFlashUser(PcPlayerForm player, PaperGameClient client) {
        super(player);
        this.client = client;
        this.lastPingTime = System.currentTimeMillis();
    }

    public long getLastPingTime() {
        return lastPingTime;
    }

    public void setLastPingTime(long lastPingTime) {
        this.lastPingTime = lastPingTime;
    }

    public void sendPacket(PaperGameServerPacket packet) {
        client.sendPacket(packet);
    }

    public void disconnect(PaperGameServerPacket msg) {
        try {
            client.getConnection().close(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isInBattle() {
        return getBattle() != null;
    }

    public void setInBattleState() {
        PaperGameClient clientTmp = client;
        if(clientTmp != null) {
            clientTmp.setState(PaperGameClient.GameClientState.IN_BATTLE);
        }
    }

    public void setNormalState() {
        PaperGameClient clientTmp = client;
        if(clientTmp != null) {
            clientTmp.setState(PaperGameClient.GameClientState.AUTHED);
        }
    }

    public boolean isDetached() {
        PaperGameClient clientTmp = client;
        return clientTmp == null || clientTmp.isDetached();
    }

    @Override
    public boolean checkPingTimeout(long timeout) {
        return getPingTimeout() > timeout;
    }

    @Override
    public long getPingTimeout() {
        return (System.currentTimeMillis() - lastPingTime);
    }

    @Override
    public NpcActionProcessor getActionProcessor() {
        return null;
    }

}
